package main;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class MainWindow extends JFrame 
{
	Client client;
	private JPanel contentPane;
	private JTextField userIDInput;
	private JPasswordField passwordInput;
	private JTextField iPInput;
	private JTextField tfport;

	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run() 
			{
				try 
				{
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
			}
		});
	}

	public MainWindow() 
	{
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 353, 239);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton bConnect = new JButton("Connect");
		bConnect.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				try 
				{
					client = new Client(iPInput.getText(), tfport.getText(), userIDInput.getText(), new String(passwordInput.getPassword()));
				} 
				catch (IllegalArgumentException e) 
				{
					e.printStackTrace();
				} 
				catch (IOException e) 
				{
					e.printStackTrace();
				}
			}
		});
		bConnect.setBounds(48, 185, 89, 23);
		contentPane.add(bConnect);
		
		JButton bDisconnect = new JButton("Disconnect");
		bDisconnect.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				client.dispose();
			}
		});
		bDisconnect.setBounds(174, 185, 108, 23);
		contentPane.add(bDisconnect);
		
		JLabel label = new JLabel("Username");
		label.setBounds(10, 99, 76, 14);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("Password");
		label_1.setBounds(10, 128, 76, 14);
		contentPane.add(label_1);
		
		userIDInput = new JTextField();
		userIDInput.setText("");
		userIDInput.setColumns(10);
		userIDInput.setBounds(112, 96, 170, 20);
		contentPane.add(userIDInput);
		
		passwordInput = new JPasswordField();
		passwordInput.setBounds(112, 125, 170, 20);
		contentPane.add(passwordInput);
		
		JLabel label_2 = new JLabel("Server Address");
		label_2.setBounds(10, 26, 89, 14);
		contentPane.add(label_2);
		
		iPInput = new JTextField();
		iPInput.setText("192.168.1.1");
		iPInput.setColumns(10);
		iPInput.setBounds(112, 23, 170, 20);
		contentPane.add(iPInput);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(11, 51, 89, 14);
		contentPane.add(lblPort);
		
		tfport = new JTextField();
		tfport.setColumns(10);
		tfport.setBounds(113, 48, 170, 20);
		contentPane.add(tfport);
	}

}
